<template>
  <el-dialog
    title="新增员工"
    :visible="showDialog"
    top="8vh"
    @close="closeDialog"
  >
    <!-- 表单 -->
    <el-form ref="addForm" :model="formData" :rules="rules" label-width="120px">
      <el-form-item prop="username" label="姓名">
        <el-input
          v-model="formData.username"
          style="width: 50%"
          placeholder="请输入姓名"
        />
      </el-form-item>
      <el-form-item prop="mobile" label="手机">
        <el-input
          v-model="formData.mobile"
          style="width: 50%"
          placeholder="请输入手机号"
        />
      </el-form-item>
      <el-form-item prop="timeOfEntry" label="入职时间">
        <el-date-picker
          v-model="formData.timeOfEntry"
          style="width: 50%"
          placeholder="请选择入职时间"
        />
      </el-form-item>
      <el-form-item prop="formOfEmployment" label="聘用形式">
        <el-select
          v-model="formData.formOfEmployment"
          style="width: 50%"
          placeholder="请选择"
        >
          <el-option
            v-for="item in dataTypes.hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="workNumber" label="工号">
        <el-input
          v-model="formData.workNumber"
          style="width: 50%"
          placeholder="请输入工号"
        />
      </el-form-item>
      <el-form-item prop="departmentName" label="部门">
        <el-input
          v-model="formData.departmentName"
          style="width: 50%"
          placeholder="请选择部门"
          @focus="showTree = true"
        />
        <!-- 选择部门 -->
        <!-- <Tree
          :visible="showTree"
          @clode-tree="close"
          @current-select="getDept"
        /> -->
        <Tree :visible.sync="showTree" @current-select="getDept" />
      </el-form-item>
      <el-form-item prop="correctionTime" label="转正时间">
        <el-date-picker
          v-model="formData.correctionTime"
          style="width: 50%"
          placeholder="请选择转正时间"
        />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-button @click="$emit('close-dialog')">取消</el-button>
      <el-button type="primary" @click="submitAdd">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
// 导入树形组织架构方法  和格式化时间方法
import { parseTime } from '@/utils/index'
// 导入添加用户接口
import { addEmployee } from '@/api/employees'
// 导入数据字典
import dataTypes from '@/api/constant/employees'
export default {
  props: { // 父组件传的值
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      dataTypes, // 数据字典-聘用形式
      showTree: false, // 部门树形结构
      treeData: [], // 定义数组接受树形数据
      formData: {
        username: '', // 用户名
        mobile: '', // 手机号
        timeOfEntry: '', // 入职时间
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        correctionTime: '' // 转正时间
      },
      rules: {
        username: [ // 用户名
          { required: true, message: '用户姓名不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 10, message: '用户姓名为1-4位', trigger: ['blur', 'change'] }
        ],
        mobile: [ // 手机号
          { required: true, message: '手机号不能为空', trigger: ['blur', 'change'] },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: ['blur'] }
        ],
        formOfEmployment: [ // 聘用形式
          { required: true, message: '聘用形式不能为空', trigger: ['blur', 'change'] }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: ['blur', 'change'] }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: ['blur', 'change'] }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  methods: {
    // 子传父 关闭弹出层
    closeDialog () {
      this.$emit('close-dialog')
      // 重置数据
      this.$refs.addForm.resetFields()
    },
    // 点击部门树形结构时 赋值部门数据
    getDept (val) {
      // 给部门赋值
      this.formData.departmentName = val.name
      // 关闭树形结构
      this.showTree = false
    },
    // 点击确定 添加员工
    submitAdd () {
      this.$refs.addForm.validate(async isOk => {
        if (isOk) {
          // 格式化时间  转正 + 入职
          this.formData.correctionTime = parseTime(this.formData.correctionTime, '{y}-{m}-{d}')
          this.formData.timeOfEntry = parseTime(this.formData.timeOfEntry, '{y}-{m}-{d}')
          // 调用接口发送数据 添加员工
          await addEmployee(this.formData)
          // 提示完成添加
          this.$message.success('完成添加')
          // 通知父关闭弹层
          this.closeDialog()
          // 通知父刷新数据
          this.$parent.getEmployeeList()
        }
      })
    }

  }
}
</script>
<style>
</style>
